﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Ruici.Web.Services.ArticleService;
using Ruici.Web.Services.CRS_Service;
using Ruici.Web.Services.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;

namespace Ruici.Web.Services
{
    public class DocumentService
    {
        private articleSoapClient articleSoap = new articleSoapClient("articleSoap");
        private CRS_ServiceSoapClient crsService = new CRS_ServiceSoapClient();
        /// <summary>
        /// 获取文章类别信息
        /// </summary>
        /// <param name="channel_name"></param>
        /// <param name="parent_id"></param>
        /// <returns></returns>
        public List<ArticleCategoryResponse> GetCategories(string channel_name,int parent_id)
        {
            DataTable categoryTable = articleSoap.get_category_list(channel_name, parent_id);
            List<ArticleCategoryResponse> categoryList = new List<ArticleCategoryResponse>();
            foreach(DataRow row in categoryTable.Rows)
            {
                ArticleCategoryResponse articleChild = new ArticleCategoryResponse();
                articleChild.id = int.Parse(row["id"].ToString());
                articleChild.channel_id = row["channel_id"].ToString();
                articleChild.title = row["title"].ToString();
                articleChild.call_index = row["call_index"].ToString();
                articleChild.parent_id = int.Parse(row["parent_id"].ToString());
                articleChild.class_list = row["class_list"].ToString();
                articleChild.class_layer = row["class_layer"].ToString();
                articleChild.sort_id = row["sort_id"].ToString();
                articleChild.link_url = row["link_url"].ToString();
                articleChild.img_url = row["img_url"].ToString();
                articleChild.content = row["content"].ToString();
                articleChild.seo_title = row["seo_title"].ToString();
                articleChild.seo_keywords = row["seo_keywords"].ToString();
                articleChild.seo_description = row["seo_description"].ToString();
                categoryList.Add(articleChild);
            }
            return categoryList;
        }
        /// <summary>
        /// 获取文章列表
        /// </summary>
        /// <param name="channel_name">频道名称</param>
        /// <param name="category_id">类别Id</param>
        /// <param name="page_size">页数</param>
        /// <param name="page_index">分页索引</param>
        /// <param name="keys">关键词</param>
        /// <param name="filter">筛选</param>
        /// <param name="totalcount">总条数</param>
        /// <returns></returns>
        public List<ArticleResponse> GetArticleList(string channel_name, int category_id, int page_size, int page_index, string keys, string filter, out int totalcount)
        {
            DataTable articlemodel = articleSoap.get_article_list(channel_name, category_id, page_size, page_index, keys, filter, out totalcount);
            List<ArticleResponse> articleResponseList = new List<ArticleResponse>();
            foreach (DataRow item in articlemodel.Rows)
            {
                ArticleResponse articleResponse = new ArticleResponse();
                articleResponse.id = Convert.ToInt32(item["id"].ToString());
                articleResponse.channel_id = Convert.ToInt32(item["channel_id"].ToString());
                articleResponse.category_id = Convert.ToInt32(item["category_id"].ToString());
                articleResponse.call_index = item["call_index"].ToString();
                articleResponse.title = item["title"].ToString();
                articleResponse.link_url = item["link_url"].ToString();
                articleResponse.img_url = item["img_url"].ToString();
                articleResponse.seo_title = item["seo_title"].ToString();
                articleResponse.seo_keywords = item["seo_keywords"].ToString();
                articleResponse.seo_description = item["seo_description"].ToString();
                articleResponse.zhaiyao = item["zhaiyao"].ToString();
                articleResponse.content = item["content"].ToString();
                articleResponse.sort_id = Convert.ToInt32(item["sort_id"].ToString());
                articleResponse.click = Convert.ToInt32(item["click"].ToString());
                articleResponse.status = Convert.ToInt32(item["status"].ToString());
                articleResponse.is_msg = Convert.ToInt32(item["is_msg"].ToString());
                articleResponse.is_top = Convert.ToInt32(item["is_top"].ToString());
                articleResponse.is_red = Convert.ToInt32(item["is_red"].ToString());
                articleResponse.is_hot = Convert.ToInt32(item["is_hot"].ToString());
                articleResponse.is_slide = Convert.ToInt32(item["is_slide"].ToString());
                articleResponse.is_sys = Convert.ToInt32(item["is_sys"].ToString());
                articleResponse.user_name = item["user_name"].ToString();
                articleResponse.add_time = Convert.ToDateTime(item["add_time"].ToString()).ToString("yyyy/MM/dd HH:mm");
                
                if (channel_name == "video")
                {
                    articleResponse.doctorID = item["doctorId"].ToString();
                    JEnumerable<JToken> DoctorInfo = GetDoctorListByNumber(Convert.ToInt32(articleResponse.doctorID));

                    if (DoctorInfo.Count() > 0)
                    {
                        foreach (JToken doc in DoctorInfo.ToList())
                        {
                            JProperty docProperty = (JProperty)doc;
                            var docValue = docProperty.First();
                            if (docProperty.Name == "name")
                                articleResponse.doctorName = docValue.ToString();
                            else if (docProperty.Name == "titleName")
                                articleResponse.doctorTitleName = docValue.ToString();
                        }
                    }
                    articleResponse.videoSrc = item["video_src"].ToString();
                } 
                else if(channel_name == "guide")
                {
                    string sPDFUrl = ConfigurationManager.AppSettings["PDFUrl"].ToString();
                    articleResponse.fileSrc = sPDFUrl + item["file_src"].ToString();
                }            
                articleResponseList.Add(articleResponse);
            }
            return articleResponseList;
        }
        /// <summary>
        /// 获取文章信息
        /// </summary>
        /// <param name="Id">文章Id</param>
        /// <returns></returns>
        public ArticleResponse GetArticleInfo(int Id)
        {
            JEnumerable<JToken> ArticleInfor;
            JEnumerable<JToken> FieldInfor;
            string sArticleDetail = articleSoap.get_article_info(Id);
            ArticleResponse articleResponse = new ArticleResponse();
            if (sArticleDetail != null)
            {
                JObject ArticleInfoArray = (JObject)JsonConvert.DeserializeObject(sArticleDetail);
                ArticleInfor = ArticleInfoArray.Children();

                if (ArticleInfor.Count() > 0)
                {
                    foreach (JToken article in ArticleInfor.ToList())
                    {
                        JProperty docProperty = (JProperty)article;
                        var docValue = docProperty.First();
                        if (docProperty.Name == "title")
                            articleResponse.title = docValue.ToString();
                        else if (docProperty.Name == "add_time")
                            articleResponse.add_time = Convert.ToDateTime(docValue.ToString()).AddHours(8).ToString("yyyy-MM-dd HH:mm");
                        else if (docProperty.Name == "content")
                            articleResponse.content = docValue.ToString();
                        else if (docProperty.Name == "fields")
                        {
                            string sFieldDetail = docValue.ToString();
                            if (sFieldDetail != null)
                            {
                                JObject FieldInfoArray = (JObject)JsonConvert.DeserializeObject(sFieldDetail);
                                FieldInfor = FieldInfoArray.Children();
                                if (FieldInfor.Count() > 0)
                                {
                                    foreach (JToken field in FieldInfor.ToList())
                                    {
                                        JProperty fieldProperty = (JProperty)field;
                                        var fieldValue = fieldProperty.First();
                                        if (fieldProperty.Name == "video_src")
                                            articleResponse.videoSrc = fieldValue.ToString();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return articleResponse;
        }
        /// <summary>
        /// 根据医生标识获取医生信息
        /// </summary>
        /// <param name="DoctorId"></param>
        /// <returns></returns>
        public JEnumerable<JToken> GetDoctorListByNumber(int DoctorId)
        {
            JEnumerable<JToken> patinetInforList;
            ReturnModel docList = crsService.getDoctor(DoctorId);
            if(docList.Data!=null)
            {
                JObject patientInfoArray = (JObject)JsonConvert.DeserializeObject(docList.Data.ToString());
                patinetInforList = patientInfoArray.Children();
            }        
            return patinetInforList;
        }
        /// <summary>
        /// 获取文章类别列表
        /// </summary>
        /// <param name="sChannelName"></param>
        /// <param name="iParentId"></param>
        /// <returns></returns>
        public Dictionary<string, List<ArticleCategoryResponse>> GetArticleCategoryList(string sChannelName, int iParentId)
        {
            DataTable CategoryDT = articleSoap.get_category_list(sChannelName, iParentId);
            Dictionary<string, List<ArticleCategoryResponse>> CategoryDTList = new Dictionary<string, List<ArticleCategoryResponse>>();
            foreach(DataRow row in CategoryDT.Rows)
            {
                ArticleCategoryResponse articleParent = new ArticleCategoryResponse();
                articleParent.title = row["title"].ToString();
                articleParent.class_list = row["class_list"].ToString();
                DataTable tempCategoryDT = new DataTable();
                tempCategoryDT = articleSoap.get_category_list(sChannelName, int.Parse(articleParent.class_list.Replace(",",string.Empty)));
                List<ArticleCategoryResponse> categoryList = new List<ArticleCategoryResponse>();
                if (tempCategoryDT.Rows.Count > 0 )
                {
                    foreach (DataRow item in tempCategoryDT.Rows)
                    {
                        ArticleCategoryResponse articleChild = new ArticleCategoryResponse();
                        articleChild.id = int.Parse(item["id"].ToString());
                        articleChild.channel_id = item["channel_id"].ToString();
                        articleChild.title = item["title"].ToString();
                        articleChild.call_index = item["call_index"].ToString();
                        articleChild.parent_id = int.Parse(item["parent_id"].ToString());
                        articleChild.class_list = item["class_list"].ToString();
                        articleChild.class_layer = item["class_layer"].ToString();
                        articleChild.sort_id = item["sort_id"].ToString();
                        articleChild.link_url = item["link_url"].ToString();
                        articleChild.img_url = item["img_url"].ToString();
                        articleChild.content = item["content"].ToString();
                        articleChild.seo_title = item["seo_title"].ToString();
                        articleChild.seo_keywords = item["seo_keywords"].ToString();
                        articleChild.seo_description = item["seo_description"].ToString();
                        categoryList.Add(articleChild);
                    }
                    
                }
                CategoryDTList.Add(articleParent.title, categoryList);
            }

            return CategoryDTList;
        }
    }
}
